package org.szgb.console.ht.electricity.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.line.entity.SysLineLocation;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class HtElectricityAmmeter extends BeanEntity<HtElectricityAmmeter> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLineLocation location;
    private String number;
    private String name;

    public HtElectricityAmmeter() {
    }

    public HtElectricityAmmeter(String id) {
        this.setId(id);
    }

    public HtElectricityAmmeter(HtElectricityAmmeter.HtElectricityAmmeterFindDto htElectricityAmmeterFindDto){
        this.setId(htElectricityAmmeterFindDto.getId());
        this.setLocation(new SysLineLocation(htElectricityAmmeterFindDto.getLocationId()));
        this.setName(htElectricityAmmeterFindDto.getName());
        this.setNumber(htElectricityAmmeterFindDto.getNumber());
        this.setPageDto(htElectricityAmmeterFindDto.getPageDto());
    }

    public HtElectricityAmmeter(HtElectricityAmmeter.HtElectricityAmmeterSaveDto htElectricityAmmeterSaveDto){
        this.setId(htElectricityAmmeterSaveDto.id);
        this.setLocation(new SysLineLocation(htElectricityAmmeterSaveDto.locationId));
        this.setName(htElectricityAmmeterSaveDto.name);
        this.setNumber(htElectricityAmmeterSaveDto.number);
    }

    public HtElectricityAmmeter.HtElectricityAmmeterView toHtElectricityAmmeterView() {
        return new HtElectricityAmmeter.HtElectricityAmmeterView(this);
    }

    @Data
    @Schema(title = "电表信息", description = "电表信息")
    public static class HtElectricityAmmeterView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "地点id")
        private String locationId;
        @Schema(description = "地点名称")
        private String locationName;
        @Schema(description = "电表号")
        private String number;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public HtElectricityAmmeterView(HtElectricityAmmeter htElectricityAmmeter) {
            this.id = htElectricityAmmeter.getId();
            this.locationId = htElectricityAmmeter.getLocation().getId();
            this.locationName = htElectricityAmmeter.getLocation().getName();
            this.number = htElectricityAmmeter.getNumber();
            this.name = htElectricityAmmeter.getName();
            this.createdAt = htElectricityAmmeter.getCreatedAt();
            this.updatedAt = htElectricityAmmeter.getUpdatedAt();
            if (htElectricityAmmeter.getCreatedBy() != null) {
                this.createdById = htElectricityAmmeter.getCreatedBy().getId();
                this.createdByName = htElectricityAmmeter.getCreatedBy().getName();
            }
            if (htElectricityAmmeter.getUpdatedBy() != null) {
                this.updatedById = htElectricityAmmeter.getUpdatedBy().getId();
                this.updatedByName = htElectricityAmmeter.getUpdatedBy().getName();
            }
        }

        public static List<HtElectricityAmmeter.HtElectricityAmmeterView> getViewListFromList(List<HtElectricityAmmeter> htElectricityAmmeterList) {
            return htElectricityAmmeterList.stream().map(HtElectricityAmmeter.HtElectricityAmmeterView::new).toList();
        }
    }

    @Data
    @Schema(title = "电表查询表单", description = "电表查询参数")
    public static class HtElectricityAmmeterFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "地点id")
        private String locationId;
        @Schema(description = "电表号")
        private String number;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public HtElectricityAmmeter toHtElectricityAmmeter() {
            return new HtElectricityAmmeter(this);
        }
    }

    @Data
    @Schema(title = "电表保存表单", description = "电表保存参数")
    public static class HtElectricityAmmeterSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "地点id")
        private String locationId;
        @Schema(description = "电表号")
        private String number;
        @Schema(description = "名称")
        private String name;

        public HtElectricityAmmeter toHtElectricityAmmeter() {
            return new HtElectricityAmmeter(this);
        }
    }
}
